package org.hxyjs.dao.impl;

import org.hxyjs.dao.RestaurantDao;
import org.hxyjs.dao.RestaurantForOrder;
import org.hxyjs.entity.Restaurant;
import org.hxyjs.mapper.RestaurantMapper;
import org.hxyjs.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("restaurantDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class RestaurantDaoImpl implements RestaurantDao , RestaurantForOrder {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private RestaurantMapper restaurantMapper;
    public List<Restaurant>  getAllRestaurants() throws Exception{
        return restaurantMapper.selectByExample(null);
    }

    @Override
    public List<Restaurant> getRestaurantByPage(PageInfo pageInfo) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount =(int) restaurantMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return restaurantMapper.getRestaurantByPage(map);
    }

//    @Override
//    public Restaurant selectRestaurantByRno(String sno) throws Exception {
//        return restaurantMapper.selectByPrimaryKey(sno);
//    }


    @Override
    public Integer deleteRestaurantByRno(String rno) {
        return restaurantMapper.deleteByPrimaryKey(rno);
    }

    @Override
    public Integer updateRestaurantByRno(Restaurant restaurant) {
        return restaurantMapper.updateByPrimaryKey(restaurant);
    }

    @Override
    public Integer addRestaurantByRno(Restaurant restaurant) {
        restaurant.setRno(initId());
        return restaurantMapper.insertSelective(restaurant);
    }
    @Override
    public Restaurant getRestaurantByRno(String rno) throws Exception{
        return restaurantMapper.selectByPrimaryKey(rno);
    }

    @Override
    public Restaurant selectRestaurantWithorderAndUser(String rno) throws Exception {
        return restaurantMapper.selectRestaurantWithorderAndUser(rno);
    }

    @Override
    public Restaurant getAccountByAccountName(String rname) throws Exception {
        return restaurantMapper.getAccountByAccountName(rname);
    }

    @Override
    public List<Restaurant> getFreeRestaurantByDate(String beginDate,String endDate) throws Exception {
        return restaurantMapper.getFreeRestaurantByDate(beginDate, endDate);
    }

    public String initId() {
        String maxRno = restaurantMapper.getMaxRno();
        return org.hxyjs.utils.GenNumTools.initId("15", 3, maxRno);
    }

}